﻿using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.IO;
using System.Net;
using System.Xml;

public partial class groups : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            bindgrid();
        }
    }
    private void bindgrid()
    {
        grdGroups.DataSource = SqlDataSource1;
        grdGroups.DataBind();
        foreach (GridViewRow grdrow in grdGroups.Rows)
        {
            Label lblgrpid = (Label)grdrow.Cells[0].FindControl("lblgrpid");
            Label lblTotalContacts = (Label)grdrow.Cells[1].FindControl("lblTotalContacts");
            Label lblGrpname = (Label)grdrow.Cells[1].FindControl("lblGrpname");
            if (lblGrpname.Text == "Opt Outs")
            {
                grdrow.Visible = false;
            }
            object totalcontacts = SqlHelper.ExecuteScalar(GlobalVariable.con, CommandType.Text, "select count(ContactId) from tbl_groupcontacts where GroupId=" + Convert.ToInt32(lblgrpid.Text) + " and Unsubscribed='false'");
            lblTotalContacts.Text = "(" + totalcontacts.ToString() + ")";
        }
        if (grdGroups.Rows.Count > 0)
        {
            lblrecords.Visible = true;
            ddlGridPages.Visible = true;
        }
        else
        {
            lblrecords.Visible = false;
            ddlGridPages.Visible = false;
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (btnSubmit.Text != "Update")
        {
            SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "insert into tbl_group (GroupName) values('" + txtGroupname.Text.Trim() + "')");
            bindgrid();
            lblMessage.Text = "Group Saved Successfully.";
        }
        else
        {
            SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "update tbl_group set GroupName='" + txtGroupname.Text.Trim() + "' where GroupId=" + Convert.ToInt32(Session["grpid"].ToString()) + "");
            bindgrid();
            lblMessage.Text = "Group Updated Successfully.";
        }
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    protected void imgbtnEditGroup_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
            int index = row.RowIndex;
            Label lblgrpid = (Label)grdGroups.Rows[index].FindControl("lblgrpid");
            Session["grpid"] = lblgrpid.Text;
            object grpname = SqlHelper.ExecuteScalar(GlobalVariable.con, CommandType.Text, "select GroupName from tbl_group where GroupId=" + Convert.ToInt32(lblgrpid.Text) + "");
            txtGroupname.Text = grpname.ToString();
            btnAddGroup_ModalPopupExtender.Show();
            btnSubmit.Text = "Update";
        }
        catch (Exception exp)
        {
 
        }
    }
    protected void lmgbtnSendText_Click(object sender, ImageClickEventArgs e)
    {
        GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
        int index = row.RowIndex;
        Label lblgrpid = (Label)grdGroups.Rows[index].FindControl("lblgrpid");
        Session["smsgrpid"] = lblgrpid.Text;
        Response.Redirect("sendsms.aspx");
    }
    protected void imgbtnContacts_Click(object sender, ImageClickEventArgs e)
    {
        GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
        int index = row.RowIndex;
        Label lblgrpid = (Label)grdGroups.Rows[index].FindControl("lblgrpid");
        Session["smsgrpid"] = lblgrpid.Text;
        Response.Redirect("contacts.aspx");
    }
    protected void grdGroups_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdGroups.PageSize = e.NewPageIndex;
        bindgrid();
    }
    protected void imgbtnDeleteGroup_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            GridViewRow row = (GridViewRow)(sender as Control).Parent.Parent;
            int index = row.RowIndex;
            Label lblgrpid = (Label)grdGroups.Rows[index].FindControl("lblgrpid");
            SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "delete from tbl_groupcontacts where GroupId=" + Convert.ToInt32(lblgrpid.Text) + "");
            SqlHelper.ExecuteNonQuery(GlobalVariable.con, CommandType.Text, "delete from tbl_group where GroupId=" + Convert.ToInt32(lblgrpid.Text) + "");
            bindgrid();
            string SelectGroup = "Group deleted successfully.";
            Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert('" + SelectGroup + "')</script>"));
        }
        catch (Exception exp)
        {
            string SelectGroup = "Some error occured. Try again later.";
            Page.Controls.Add(new LiteralControl("<script language='javascript'> window.alert('" + SelectGroup + "')</script>"));
        }
    }
    protected void ddlGridPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        //ddlGridPages.PageSize = Convert.ToInt32(ddlGridPages.SelectedValue);
        //bindgrid();
    }
}
